package com.chinadci.smartcommunitysystembackstage.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinadci.smartcommunitysystembackstage.entity.ScsAdmin;
import com.baomidou.mybatisplus.extension.service.IService;
import com.chinadci.smartcommunitysystembackstage.mapper.ScsActivityMapper;
import com.chinadci.smartcommunitysystembackstage.mapper.ScsAdminMapper;
import com.chinadci.smartcommunitysystembackstage.util.MD5;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author Code Duck
 * @since 2021-04-13
 */
@Service
public class ScsAdminService{
    @Autowired
    private ScsAdminMapper mapper;

    private Logger log= LoggerFactory.getLogger(this.getClass());

    //管理员登录
    public ScsAdmin Login(ScsAdmin scs){
        log.info("=====管理员登陆");
        String pwd=MD5.getMD5(scs.getAPwd(),18);
        QueryWrapper<ScsAdmin> qw=new QueryWrapper<>();
        qw.eq("a_phone",scs.getAPhone()).and(wrapper->wrapper.eq("a_pwd",pwd));
        return mapper.selectOne(qw);
    }

    //添加管理员
    public int Add(ScsAdmin scs){
        log.info("=====添加管理员");
        String pwd= MD5.getMD5(scs.getAPwd(),18);
        scs.setAPwd(pwd);
        return mapper.insert(scs);
    }

    //查询该手机号是否注册过
    public ScsAdmin Zc(ScsAdmin scs){
        log.info("=====查询改手机号是否注册过");
        QueryWrapper<ScsAdmin> qw=new QueryWrapper<>();
        qw.eq("a_phone",scs.getAPhone());
        return mapper.selectOne(qw);
    }

    //查询所有管理员信息
    public List<ScsAdmin> Find(String name){
        log.info("=====查询所有管理员信息");
        List<ScsAdmin> list=new ArrayList<>();
        if (name!=null && name!=""){
            list=mapper.Find("%"+name+"%");
        }else{
            list=mapper.Find("%%");
        }
        for (ScsAdmin item : list) {
            item.setAtype(item.getType().getTTypename());
        }
        return list;
    }

    //删除指定管理员
    public int Del(int id){
        log.info("=====删除指定管理员");
        QueryWrapper<ScsAdmin> qw=new QueryWrapper<>();
        qw.eq("a_id",id);
        return mapper.delete(qw);
    }

    //修改指定管理员
    public int Update(ScsAdmin scs){
        log.info("=====修改指定管理员");
        return mapper.updateById(scs);
    }

    //修改管理员信息查询
    public List<ScsAdmin> Find(int id){
        log.info("=====修改管理员信息查询");
        List<ScsAdmin> list=new ArrayList<>();
        list= mapper.Finds(id);
        for (ScsAdmin item : list) {
            item.setAtype(item.getType().getTTypename());
        }
        return list;
    }

    //报修根据id查找管理员姓名
    public ScsAdmin find(int id){
        log.info("=====报修根据id查找管理员姓名");
        QueryWrapper<ScsAdmin> qw=new QueryWrapper<>();
        qw.eq("a_id",id);
        return mapper.selectOne(qw);
    }

    //添加社区公告根据姓名查找管理员id
    public ScsAdmin find(String name){
        log.info("=====报修根据id查找管理员姓名");
        QueryWrapper<ScsAdmin> qw=new QueryWrapper<>();
        qw.eq("a_name",name);
        return mapper.selectOne(qw);
    }

}
